using System;
using JmShared;
using System.Collections.Generic;
using Gtk;
using System.Linq;

namespace JiveMessengerLinux
{
    public partial class PrivateMessage : Gtk.Window
    {
        private UserInfo PMTarget { get; set; }

        private bool _endSession;

        public event EventHandler<WriteMessageEventArgs> WriteMessageEvent;
        public event EventHandler<ClosePMEventArgs> ClosePmEvent;
        public event EventHandler<EventArgs> NowWritingEvent;

        public PrivateMessage(UserInfo info) : 
				base(Gtk.WindowType.Toplevel)
        {
            this.Build();
            this.PMTarget = info;
            this.Title = PMTarget.PrettyName;


            this.ChatControl.Data.Add(this.PMTarget.UserID, this.PMTarget);

            this.ChatControl.NowWritingEvent += (sender, e) => 
            {
                NowWritingEvent(this, null);
            };
            this.ChatControl.WriteMessageEvent += (sender, e) => 
            {

                ChatMessage msg = e.Message;
                msg.MessageType = JmShared.MessageType.PM;
                msg.UserInfo[0] = PMTarget;
                msg.Sender = Client.CurrentUserInfo;
                WriteMessageEvent(this, new WriteMessageEventArgs(msg));
        
            };
            this.ChatControl.FocusMessageBox();
        }

        public void SetStatus(bool offline)
        {

            if (offline)
            {
                this.Title = String.IsNullOrEmpty(this.PMTarget.NickName) ? this.PMTarget.UserName : this.PMTarget.NickName + "<" + this.PMTarget.UserName + "> -Offline-";
                this.ChatControl.DisableMessageBox();
//                this.ChatControl.bnNudge.Visibility = Visibility.Hidden;
                this._endSession = true;
            }
            else
            {
                this.Title = String.IsNullOrEmpty(this.PMTarget.NickName) ? this.PMTarget.UserName : this.PMTarget.NickName + "<" + this.PMTarget.UserName + ">";
                this.ChatControl.EnableMessageBox();
//                this.ChatControl.bnNudge.Visibility = Visibility.Visible;

            }
        }

        public Dictionary<string, DateTime> LastReceivedMessageTyping
        {
            get{ return ChatControl.LastReceivedMessageTyping;}
        }

        public void DisplayMessage(ChatMessage msg)
        {
            DisplayMessage(msg.Message, msg.Sender, msg.MessageType);
        }
        public void DisplayMessage(object obj, UserInfo user, JmShared.MessageType mt)
        {
            // WriteMessage(msg.Message, msg.Sender, MessageType.PM);
            this.ChatControl.DisplayMessage(obj, user, mt);
            //            if (!this.IsActive && Properties.Settings.Default.SettingFlashWindowOnNewMessage)
            //            {
            //                this.FlashWindow();
            //                IsFlashing = true;
            //            }
            if (this._endSession)
                this.ChatControl.SessionID++;
        }
        protected void OnDeleteEvent(object sender, DeleteEventArgs e)
        {
            UserInfo user = (UserInfo)this.ChatControl.Data.Values.Cast<UserInfo>().ToList().First();

            //TextMethods.WriteLog(user.UserName, this.ChatControl.Log);
            this.ChatControl.WriteLog(user.UserName);
            ClosePmEvent(this, new ClosePMEventArgs(PMTarget));
        }
    }
}

